using System.IO;
using System.Text;
using System.Windows.Media.Imaging;
using Legacy.Core.Api;
using Legacy.Core.SaveGameManagement;

namespace MMXSaveGameConverter
{
    internal abstract class FileWriterBase
    {
        protected readonly SaveGameViewItem Item;
        protected readonly SaveGame SaveGame;

        protected FileWriterBase(SaveGameViewItem item)
        {
            Item = item;
            SaveGame = Item.SaveGame;

            SaveGameMeta meta = SaveGame.GetMeta();
            LegacyLogic.Instance.WorldManager.HighestSaveGameNumber = meta.SaveNumber;
            LegacyLogic.Instance.WorldManager.CurrentSaveGameType = meta.Type;
        }

        public abstract void Write();

        protected virtual void WriteSaveGame(Stream output, string saveName)
        {
            SaveGame.SetName(saveName);
            BinaryWriter bw = new BinaryWriter(output, Encoding.Default);
            SaveGame.Write(bw);
        }

        protected virtual void WriteScreenshot(Stream output)
        {
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(Item.Screenshot);
            encoder.Save(output);
        }
    }
}